package org.linlinjava.litemall.brand.web;

import com.github.pagehelper.PageInfo;
import org.linlinjava.litemall.brand.annotation.LoginUser;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallDisputeOrder;
import org.linlinjava.litemall.db.domain.LitemallOrder;
import org.linlinjava.litemall.db.domain.LitemallOrderGoods;
import org.linlinjava.litemall.db.service.LitemallDisputeService;
import org.linlinjava.litemall.db.service.LitemallOrderGoodsService;
import org.linlinjava.litemall.db.service.LitemallOrderService;
import org.linlinjava.litemall.db.service.LitemallServiceService;
import org.linlinjava.litemall.db.util.OrderConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * dispute争议单
 */
@RestController
@RequestMapping("brand/dispute")
public class BrandDisputeController {
    @Autowired
    LitemallDisputeService disputeService;
    @Autowired
    LitemallOrderService orderService;
    @Autowired
    LitemallOrderGoodsService orderGoodsService;
    //serviceService 是用商户ID和 商户首字母缩写 来 找到 商户 ID
    @Autowired
    LitemallServiceService serviceService;

    /**
     * 显示商户的 争议单
     * @param userId  userId 是商户的ID brandId
     * @param msg   page  pageSize
     * @return
     */
    @PostMapping("list")
    public Object list(@LoginUser Integer userId, @RequestBody String msg) {
        Integer page = JacksonUtil.parseInteger(msg, "page");
        Integer pageSize = JacksonUtil.parseInteger(msg, "pageSize");
        if (page == null || pageSize == null) {
            return ResponseUtil.badArgument();
        }
        if (page < 0 || pageSize < 0) {
            return ResponseUtil.badArgumentValue();
        }
        Integer brandId = serviceService.getBelong(userId);
        PageInfo<LitemallDisputeOrder> orderPageInfo = disputeService.queryOrder(null, brandId, page, pageSize);
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("list", orderPageInfo.getList());
        map.put("totalNum", orderPageInfo.getTotal());
        return ResponseUtil.ok(map);
    }

    /**
     *  回复是否同意退款
     * @param msg   disputeId争议ID   agree同意否   reason 原由  picList多张图片
     * @param userId
     * @return
     */
    @PostMapping("reply")
    public Object reply(@RequestBody String msg, @LoginUser Integer userId) {
        Integer disputeId = JacksonUtil.parseInteger(msg, "disputeId");
        Boolean agree = JacksonUtil.parseBoolean(msg, "agree");
        String reason  = JacksonUtil.parseString(msg, "reason");
        List<String> picList = JacksonUtil.parseStringList(msg, "picList");
        if (disputeId == null || agree == null) {
            return ResponseUtil.badArgument();
        }
        Integer brandId = serviceService.getBelong(userId);
        LitemallDisputeOrder disputeOrder = disputeService.findOrderById(disputeId);
        if (disputeOrder == null || !disputeOrder.getBrandId().equals(brandId)) {
            return ResponseUtil.unauthz();
        }
        //如果同意退款，已完成
        if (agree) {
            LitemallOrder order = orderService.findById(disputeOrder.getOrderId());
            order.setOrderStatus(OrderConstant.COMPLETED);
            orderService.updateWithOptimisticLocker(order);
        }
        disputeService.replyDispute(disputeOrder, reason, picList, agree);
        return ResponseUtil.ok();
    }
}
